package com.networklib.http.other;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Build.VERSION;
import android.telephony.TelephonyManager;

public class NetUtil {

    public static NetworkInfo isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            int var4;
            int var5;
            if (VERSION.SDK_INT >= 21) {
                Network[] networkInfos = cm.getAllNetworks();
                if (networkInfos != null) {
                    Network[] var3 = networkInfos;
                    var4 = networkInfos.length;

                    for (var5 = 0; var5 < var4; ++var5) {
                        Network networkInfo = var3[var5];
                        NetworkInfo networkInfo1 = cm.getNetworkInfo(networkInfo);
                        if (networkInfo1.isAvailable()) {
                            return networkInfo1;
                        }
                    }
                }
            } else {
                NetworkInfo[] var8 = cm.getAllNetworkInfo();
                NetworkInfo[] var9 = var8;
                var4 = var8.length;

                for (var5 = 0; var5 < var4; ++var5) {
                    NetworkInfo var10 = var9[var5];
                    if (var10.isAvailable()) {
                        return var10;
                    }
                }
            }
        }

        return null;
    }

    public static boolean activeNetworkIsAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm != null && cm.getActiveNetworkInfo().isAvailable();
    }

    public static boolean isWifiEnabled(Context context) {
        ConnectivityManager mgrConn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager mgrTel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return mgrConn.getActiveNetworkInfo() != null && mgrConn.getActiveNetworkInfo().getState() == State.CONNECTED || mgrTel.getNetworkType() == 3;
    }

    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        return networkINfo != null && networkINfo.getType() == 1;
    }

    public static boolean isMobilNet(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        return networkINfo != null && networkINfo.getType() == 0;
    }
}
